#!/usr/bin/env python
#-*- encoding:utf-8 -*-
#
#       Update Notifier launcher
#
#       Kévin Gomez <geek63@gmail.com>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

from PyQt4 import QtGui, QtCore
import pynotify
import sys

from gui.SysTrayIcon import SysTray
from libs.Checker import Checker
from libs.Config import Config, USER_CONFIG_FILE, ICONS_DIR


class UpdateNotifier(QtGui.QApplication):
    def __init__(self):
        QtGui.QApplication.__init__(self, sys.argv)
        QtGui.QApplication.setQuitOnLastWindowClosed(False)

        self.conf = Config(USER_CONFIG_FILE)
        self.sys_tray = SysTray()
        self.timer = QtCore.QTimer()
        self.updateInterval()

        self.createActions()

    def createActions(self):
        self.connect(self.timer, QtCore.SIGNAL('timeout()'), self.check)
        self.connect(self.sys_tray, QtCore.SIGNAL('check()'), lambda: self.check(True))
        self.connect(self.sys_tray, QtCore.SIGNAL('exit()'), self.quit)
        self.connect(self.sys_tray, QtCore.SIGNAL('prefsModified()'),
                     lambda: self.conf.getConf(True))
        self.connect(self.sys_tray, QtCore.SIGNAL('prefsModified()'),
                     self.updateInterval)
        self.connect(self, QtCore.SIGNAL('setIcon'), self.sys_tray.setIcon)

    def check(self, clicked=False):
        self.emit(QtCore.SIGNAL('setIcon'), ICONS_DIR+'/pacman_orange.png')

        Checker.checkForUpdates(lambda x,y,z: self.afterCheck(x, y, z, clicked))

    def afterCheck(self, nb_updates, updates_list, size, clicked=False):
        if nb_updates:
            self.emit(QtCore.SIGNAL('setIcon'), ICONS_DIR+'/pacman_red.png')
            self.notifyUpdates(nb_updates, updates_list, size)
        else:
            self.emit(QtCore.SIGNAL('setIcon'), ICONS_DIR+'/pacman.png')

            if clicked:
                self.notifyNoUpdates()

    def notifyUpdates(self, nb_updates, updates_l, size):

        title = self.tr(('Update Notifier :: %d mise(s) à jour '+
                         'disponible(s)') % nb_updates)
        content = self.tr('%s \n\n Taille du téléchargement : %s'%
                          (', '.join(updates_l), size))

        self._notify(title, content)

    def notifyNoUpdates(self):
        self._notify(self.tr('Update Notifier :: Pas de mises à jour disponibles'),
                     self.tr('Pas de mises à jour disponibles'))

    def _notify(self, title, content):
        pynotify.init('update_checker')

        notification = pynotify.Notification(unicode(title), unicode(content))
        notification.set_urgency(pynotify.URGENCY_NORMAL)
        notification.set_timeout(self.conf.getInt('notif_duration'))

        notification.show()

    def updateInterval(self):
        self.timer.setInterval(self.conf.getInt('check_interval'))

    def run(self):
        self.timer.start()
        self.sys_tray.show()
        self.exec_()


if __name__ == '__main__':
    main = UpdateNotifier()
    main.run()